package com.eden.door.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eden.door.context.AppContext;
import com.eden.door.entity.Door;
import com.eden.door.entity.DoorAttr;
import com.eden.door.util.DoorUtil;
import com.eden.door.view.DoorView;
import com.eden.door.view.MessageBox;

/**
 * 制单按钮,将数据添加到表格  
 * 自身实现监听器接口,处理点击时事件
 * @author Eden
 * @version 1.0
 */
public class AddOrderButton extends JButton implements ActionListener {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(CalculateButton.class);

	//门的序号,维护着一个Map,key为不同的门的code,value为已添加门的总数
	private Map<String, Integer> orderNumMap;

	private JTable table ;
	private Door door ;
	private Map<String, DoorAttr> doorAttrMap;
	private DoorView doorView ;
	
	
	/**
	 * 构造方法 
	 * @param name 铵钮名称
	 */
	public AddOrderButton(String name) {
		super(name);
		this.addActionListener(this);
	}

	/**
	 * 点击按钮时的操作
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		log.debug("取出当前显示的门页面");
		
		doorView = AppContext.instance().getCurrDoorView();
		door = doorView.getDoorWithValue();
		doorAttrMap = door.getDoorAttrs();
		table = doorView.getTable();
		
		TableModel model = table.getModel();
		int columnCount = model.getColumnCount();

		
		String cellData = "";
		String columnName = "";
		
		//向表中添加一行时用到的数据
		Object[] data = new Object[columnCount] ;
		//向一行的最后加入删除按钮
		ImageIcon deleteButton = new ImageIcon("image/rowdel.png") ;
		data[columnCount - 1] = deleteButton ;
		//下单时该门的序号
		int rowNum = getDoorNum(door.getCode());
		data[0] = rowNum ;
		
		try{
			//向数据中添加计算结果
			for (int i = 1; i < columnCount - 1; i++) {
				columnName = model.getColumnName(i);
				data[i] = DoorUtil.getTextByName(columnName, doorAttrMap) ;
			}
		} catch( Exception e1) {
			MessageBox.showMsgBox("请计算之后再添加到表格") ;
			return ;
		}
		DefaultTableModel tableModel = ((DefaultTableModel)table.getModel()) ; 
		tableModel.insertRow(tableModel.getRowCount() - 10, data) ;
		
		table.setRowSelectionAllowed(true) ;
		table.getColumnModel().getColumn(columnCount-1).setCellRenderer(new ImageRenderer()) ;
	}

	/**
	 * 获取门序号
	 * @param code
	 * @return
	 */
	public int getDoorNum(String code) {
		int num = 0;
		int row = this.table.getRowCount() ;
		/*if (orderNumMap == null) {
			orderNumMap = new HashMap<String, Integer>();
		}

		if (orderNumMap.get(code) == null) {
			orderNumMap.put(code, num + 1);
		} else {
			num = orderNumMap.get(code);
			orderNumMap.put(code, num + 1);
		}*/

		if(row < 11) return 0 ;
		Object v = this.table.getValueAt(row - 11, 0) ;
		if(v == null) {
			return 0 ;
		} else {
			try{
				num = Integer.valueOf(v.toString()) ;
			} catch(NumberFormatException e){
				return 0 ;
			}
		}
		return ++num;
	}
}

